package com.gtxd.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gtxd.entity.Doctorinfo;
import com.gtxd.entity.Patientinfo;
import com.gtxd.utils.PageParams;
import com.gtxd.utils.PageUtils;
import com.gtxd.utils.R;
import com.gtxd.utils.gson.PerfectGson;

/**
 * InnoDB free: 233472 kB
 * 
 * @author yangcheng
 * @email 717449318@qq.com
 * @date 2018-03-18 08:57:56
 */
@RestController
@RequestMapping("doctorinfo")
public class DoctorinfoController extends BaseControl { 

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) throws Exception {
		// 查询列表数据
		PageParams query = new PageParams(params);
		// 获取完美gson
		Gson gson = PerfectGson.getGson();

		List<Object> doctorinfoList = commonService.query(new Doctorinfo(), query);
		// json转为带泛型的list
		List<Doctorinfo> reList = gson.fromJson(gson.toJson(doctorinfoList), new TypeToken<List<Doctorinfo>>() {
		}.getType());
		int total = (int) commonService.queryRecordNum(new Doctorinfo(), "", "");

		PageUtils pageUtil = new PageUtils(reList, total, query.getPageSize(), query.getCurrentPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/combox")
	public R combox() throws Exception {
		// 获取完美gson

		List<Object> doctorinfoList = commonService
				.queryByHql("select CONCAT(dno,',',department),dname from Doctorinfo");
		doctorinfoList.add(0, new String[] { "0,0", "==请选择医生==" });

		return R.ok().put("reList", doctorinfoList);
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/screening")
	public R screening(@RequestParam Map<String, Object> params) throws Exception {

		return doctorinfoService.screening(params);

	}

	/**
	 * 信息
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/info/{dno}")
	public R info(@PathVariable("dno") String dno) throws Exception {
		// 获取完美gson
		Gson gson = PerfectGson.getGson();
		// json转为带泛型的list
		Doctorinfo doctorinfo = gson.fromJson(gson.toJson(commonService.query(dno, new Doctorinfo())),
				new TypeToken<Doctorinfo>() {
				}.getType());
		return R.ok().put("doctorinfo", doctorinfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody Doctorinfo doctorinfo) throws Exception {
		// 重名判断
		if (commonService.queryRecordNum(new Doctorinfo(), "dname", doctorinfo.getDname()) > 0)
			doctorinfo.setDname(doctorinfo.getDname() + "(" + doctorinfo.getDepartment() + ")");
		commonService.save(doctorinfo);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody Doctorinfo doctorinfo) throws Exception {
		commonService.update(doctorinfo);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] dnos) throws Exception {
		// 删除医生信息
		commonService.deletebatch(new Doctorinfo(), dnos);
		// 删除就诊信息
		patientinfoService.deletebatch(new Patientinfo(), dnos, "dno");

		return R.ok();
	}

}
